package com.hzcx.app.simplechat.ui.user;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.friend.bean.FriendLableListBean;
import com.hzcx.app.simplechat.ui.publicui.PublicConfirmDialog;
import com.hzcx.app.simplechat.ui.user.adapter.LableFlowAdapter;
import com.hzcx.app.simplechat.ui.user.bean.UserInfoBean;
import com.hzcx.app.simplechat.ui.user.contract.UserLableSettingContract;
import com.hzcx.app.simplechat.ui.user.dialog.LableConfirmDialog;
import com.hzcx.app.simplechat.ui.user.event.UserLableEvent;
import com.hzcx.app.simplechat.ui.user.presenter.UserLableSettingPresenter;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.util.log.LogUtils;
import com.hzcx.app.simplechat.view.flowlayout.FlowLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 用户标签设置界面
 */
public class UserLableSettingActivity extends BaseActivity<UserLableSettingPresenter> implements UserLableSettingContract.View {

    public static final String INTENT_USERINFO = "INTENT_USERINFO";
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.flow_layout_edit)
    FlowLayout flowLayoutEdit;
    @BindView(R.id.flow_layout_list)
    FlowLayout flowLayoutList;


    private List<FriendLableListBean> lableData;
    private LableFlowAdapter flowAdapter;
    private List<FriendLableListBean> editLableData;
    private LableFlowAdapter editFlowAdapter;
    private UserInfoBean userInfoBean;

    @Override
    public int getLayout() {
        return R.layout.activity_user_lable_setting;
    }

    @Override
    public void initPresenter() {
        mPresenter = new UserLableSettingPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("设置标签");
        lableData = new ArrayList<>();
        flowAdapter = new LableFlowAdapter();

        editLableData = new ArrayList<>();
        editFlowAdapter = new LableFlowAdapter();
    }

    @Override
    public void initData() {

        userInfoBean = (UserInfoBean) getIntent().getSerializableExtra(INTENT_USERINFO);
        if (userInfoBean != null && userInfoBean.getLable_name_arr() != null && userInfoBean.getLable_name_arr().size() > 0) {
            editLableData.addAll(userInfoBean.getLable_name_arr());
        }

        mPresenter.getLableList(this);

        flowAdapter.setOnItemOnClickListener(new LableFlowAdapter.OnItemOnClickListener() {
            @Override
            public void itemOnClick(int pos) {
                if (lableData.get(pos).isChecked()) {
                    for (int i = 0; i < editLableData.size(); i++) {
                        if (editLableData == null || EmptyUtils.isEmpty(editLableData.get(i).getName())) {
                            continue;
                        }
                        if (editLableData.get(i).getName().equals(lableData.get(pos).getName())) {
                            editLableData.remove(i);
                            break;
                        }
                    }
                } else {
                    editLableData.add(0, lableData.get(pos));
                }
                lableData.get(pos).setChecked(!lableData.get(pos).isChecked());
                flowLayoutList.setAdapter(lableData, R.layout.rv_item_lable_flow, flowAdapter);
                flowLayoutEdit.setAdapter(editLableData, R.layout.rv_item_lable_flow, editFlowAdapter);
            }

            @Override
            public void editEnter(String content, int pos) {

            }
        });

        editFlowAdapter.setOnItemOnClickListener(new LableFlowAdapter.OnItemOnClickListener() {
            @Override
            public void itemOnClick(int pos) {
                for (FriendLableListBean bean : lableData) {
                    if (bean.getName().equals(editLableData.get(pos).getName())) {
                        bean.setChecked(false);
                    }
                }
                editLableData.remove(pos);
                flowLayoutList.setAdapter(lableData, R.layout.rv_item_lable_flow, flowAdapter);
                flowLayoutEdit.setAdapter(editLableData, R.layout.rv_item_lable_flow, editFlowAdapter);
            }

            @Override
            public void editEnter(String content, int pos) {
                for (FriendLableListBean bean : lableData) {
                    if (bean.getName().equals(content)) {
                        showError("标签名称不能重复");
                        return;
                    }
                }
                editLableData.get(pos).setName(content);
                editLableData.add(new FriendLableListBean());
                flowLayoutEdit.setAdapter(editLableData, R.layout.rv_item_lable_flow, editFlowAdapter);
            }
        });
    }

    @OnClick({R.id.tv_title_right, R.id.tv_cancel})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_title_right:
                if (EmptyUtils.isNotEmpty(editFlowAdapter.getLastConent())) {
                    editLableData.get(editLableData.size() - 1).setName(editFlowAdapter.getLastConent());
                }
                LogUtils.d("content:" + editFlowAdapter.getLastConent());
                LogUtils.d(new Gson().toJson(editLableData));
                mPresenter.setUserLable(this, userInfoBean.getMember_id(), editLableData);
                break;

            case R.id.tv_cancel:
                cancleLable();
                break;
        }
    }

    private void cancleLable() {
        LableConfirmDialog confirmDialog = new LableConfirmDialog(this, null, "保存本次编辑？", "保存", new LableConfirmDialog.OnLableConfirmClickListener() {
            @Override
            public void publicConfirm() {
                if (EmptyUtils.isNotEmpty(editFlowAdapter.getLastConent())) {
                    editLableData.get(editLableData.size() - 1).setName(editFlowAdapter.getLastConent());
                }
                mPresenter.setUserLable(UserLableSettingActivity.this, userInfoBean.getMember_id(), editLableData);
            }

            @Override
            public void publicCancel() {
                finish();
            }
        });
        confirmDialog.show();
    }

    /**
     * 标签列表获取成功
     *
     * @param data
     */
    @Override
    public void lableResult(List<FriendLableListBean> data) {
        try {
            tvTitleRight.setText("完成");
            lableData.clear();
            lableData.addAll(data);

            // 设置选中的标签
            for (FriendLableListBean bean : editLableData) {
                bean.setChecked(true);
                for (FriendLableListBean subBean : lableData) {
                    if (bean.getName().equals(subBean.getName())) {
                        subBean.setChecked(true);
                    }
                }
            }

            flowLayoutList.setAdapter(lableData, R.layout.rv_item_lable_flow, flowAdapter);

            // 添加默认值
            editLableData.add(new FriendLableListBean());
            flowLayoutEdit.setAdapter(editLableData, R.layout.rv_item_lable_flow, editFlowAdapter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setSuccess() {
        if (EmptyUtils.isEmpty(editLableData.get(editLableData.size() - 1).getName())) {
            editLableData.remove(editLableData.size() - 1);
        }
        EventBus.getDefault().post(new UserLableEvent(editLableData));
        showError("设置成功");
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
